﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace AnimalWars2
{
    public class MapPlayer
    {
        private GameMap m_GameMap;

        private AnimatedTexture m_PlayerUpAnimatedTexture;
        private AnimatedTexture m_PlayerDownAnimatedTexture;
        private AnimatedTexture m_PlayerSideAnimatedTexture;
        private int m_PlayerAnimationFrameCount = 2;
        private int m_PlayerAnimationFrameRate = 10;

        private Vector2 m_PlayerPos = new Vector2(381, 442);
        private int m_CurrentMapSpotLine = 0, m_CurrentMapSpotColumn = 1;
        private const int m_MapLineSize = 6, m_MapColumnSize = 3;
        private int[,] m_MapSpotArray = new int[m_MapLineSize, m_MapColumnSize] { { 0, 1, 0 }, 
                                                       { 0, 1, 2 }, 
                                                       { 2, 1, 0 }, 
                                                       { 0, 1, 2 }, 
                                                       { 2, 1, 0 }, 
                                                       { 0, 2, 0 } };
        private int m_VerticalMoveDistance = 41;
        private int m_HorizontalMoveDistance = 119;
        private Vector2 m_MoveDir = Vector2.Zero;
        private int m_MoveDirVel = 3;
        private Vector2 m_MoveDirSum = Vector2.Zero;

        public MapPlayer(GameMap GameMapRef)
        {
            m_GameMap = GameMapRef;
            m_PlayerUpAnimatedTexture = new AnimatedTexture(Vector2.Zero, 0, 1.0f, 0.5f);
            m_PlayerDownAnimatedTexture = new AnimatedTexture(Vector2.Zero, 0, 1.0f, 0.5f);
            m_PlayerSideAnimatedTexture = new AnimatedTexture(Vector2.Zero, 0, 1.0f, 0.5f);
        }

        public void LoadContent()
        {
            m_PlayerUpAnimatedTexture.Load(m_GameMap.m_Game.Content, "map_pinguim_up", m_PlayerAnimationFrameCount, m_PlayerAnimationFrameRate);
            m_PlayerDownAnimatedTexture.Load(m_GameMap.m_Game.Content, "map_pinguim_down", m_PlayerAnimationFrameCount, m_PlayerAnimationFrameRate);
            m_PlayerSideAnimatedTexture.Load(m_GameMap.m_Game.Content, "map_pinguim_side", m_PlayerAnimationFrameCount, m_PlayerAnimationFrameRate);            

            m_VerticalMoveDistance += m_PlayerUpAnimatedTexture.m_Texture.Height;
            m_HorizontalMoveDistance += m_PlayerUpAnimatedTexture.m_Texture.Width / m_PlayerAnimationFrameCount;

            m_PlayerUpAnimatedTexture.Play();
        }

        private void StopPlayAnimations() {
            m_PlayerUpAnimatedTexture.Stop();
            m_PlayerDownAnimatedTexture.Stop();
            m_PlayerSideAnimatedTexture.Stop();
        }

        private void MovePlayer()
        {
            // se o player não esta movendo
            if (m_MoveDir == Vector2.Zero)
            {
                KeyboardState oKS = Keyboard.GetState();

                if (oKS.IsKeyDown(Keys.Space) && m_MapSpotArray[m_CurrentMapSpotLine, m_CurrentMapSpotColumn] == 2)
                {
                    m_GameMap.GoToTable();
                }
                else if (oKS.IsKeyDown(Keys.Up))
                {
                    if (m_CurrentMapSpotLine < m_MapLineSize - 1)
                    {
                        if (m_MapSpotArray[m_CurrentMapSpotLine + 1, m_CurrentMapSpotColumn] > 0)
                        {
                            m_CurrentMapSpotLine = m_CurrentMapSpotLine + 1;
                            m_MoveDir = new Vector2(0, -m_MoveDirVel);

                            StopPlayAnimations();
                            m_PlayerUpAnimatedTexture.Play();                            
                        }
                    }
                }
                else if (oKS.IsKeyDown(Keys.Down))
                {
                    if (m_CurrentMapSpotLine > 0)
                    {
                        if (m_MapSpotArray[m_CurrentMapSpotLine - 1, m_CurrentMapSpotColumn] > 0)
                        {
                            m_CurrentMapSpotLine = m_CurrentMapSpotLine - 1;
                            m_MoveDir = new Vector2(0, m_MoveDirVel);

                            StopPlayAnimations();
                            m_PlayerDownAnimatedTexture.Play();
                        }
                    }
                }
                else if (oKS.IsKeyDown(Keys.Left))
                {
                    if (m_CurrentMapSpotColumn > 0)
                    {
                        if (m_MapSpotArray[m_CurrentMapSpotLine, m_CurrentMapSpotColumn - 1] > 0)
                        {
                            m_CurrentMapSpotColumn = m_CurrentMapSpotColumn - 1;
                            m_MoveDir = new Vector2(-m_MoveDirVel, 0);

                            StopPlayAnimations();
                            m_PlayerSideAnimatedTexture.m_SpriteEffects = SpriteEffects.FlipHorizontally;
                            m_PlayerSideAnimatedTexture.Play();
                        }
                    }
                }
                else if (oKS.IsKeyDown(Keys.Right))
                {
                    if (m_CurrentMapSpotColumn < m_MapColumnSize - 1)
                    {
                        if (m_MapSpotArray[m_CurrentMapSpotLine, m_CurrentMapSpotColumn + 1] > 0)
                        {
                            m_CurrentMapSpotColumn = m_CurrentMapSpotColumn + 1;
                            m_MoveDir = new Vector2(m_MoveDirVel, 0);

                            StopPlayAnimations();
                            m_PlayerSideAnimatedTexture.m_SpriteEffects = SpriteEffects.None;
                            m_PlayerSideAnimatedTexture.Play();
                        }
                    }
                }
            }
            else
            {
                m_MoveDirSum += m_MoveDir;

                if (Math.Abs(m_MoveDirSum.X) < m_HorizontalMoveDistance && Math.Abs(m_MoveDirSum.Y) < m_VerticalMoveDistance)
                {
                    m_PlayerPos += m_MoveDir;
                }
                else
                {
                    if (Math.Abs(m_MoveDirSum.X) > 0)
                    {
                        m_PlayerPos.X += (Math.Abs(m_MoveDirSum.X) - m_HorizontalMoveDistance) * Math.Sign(m_MoveDirSum.X);
                    }
                    else if (Math.Abs(m_MoveDirSum.Y) > 0)
                    {
                        m_PlayerPos.Y += (Math.Abs(m_MoveDirSum.Y) - m_VerticalMoveDistance) * Math.Sign(m_MoveDirSum.Y);
                    }

                    m_MoveDir = Vector2.Zero;
                    m_MoveDirSum = Vector2.Zero;
                }
            }
        }

        public void Update(GameTime gameTime)
        {
            float elapsed = (float) gameTime.ElapsedGameTime.TotalSeconds;

            m_PlayerUpAnimatedTexture.UpdateFrame(elapsed);
            m_PlayerDownAnimatedTexture.UpdateFrame(elapsed);
            m_PlayerSideAnimatedTexture.UpdateFrame(elapsed);
            
            MovePlayer();
        }

        public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            m_PlayerUpAnimatedTexture.DrawFrame(spriteBatch, m_PlayerPos);
            m_PlayerDownAnimatedTexture.DrawFrame(spriteBatch, m_PlayerPos);
            m_PlayerSideAnimatedTexture.DrawFrame(spriteBatch, m_PlayerPos);
        }
    }
}
